/**
 * Created by gongxun on 16/11/19.
 */

var KwxCard = cc.Sprite.extend({
    mCardNum : null,
    mNumSprite : null,
    mFlagSprite : null,
    mCardZOrder : -1,
    mKind : null,
    mSeat : null,
    mCreatIndex : null,
    mIsColor : false,
    mIsLiangCard : false,
    ctor : function () {
        this._super();

        this.mNumSprite = new cc.Sprite();
        this.addChild(this.mNumSprite);

        this.mFlagSprite = new cc.Sprite();
        this.mFlagSprite.setPosition(cc.p(20, 120));
        this.addChild(this.mFlagSprite);
    },

    onEnter : function(){
        this._super();
        mo.eventHelper.add("changeKwxCardBg", this, function(){
            this.setCardBg();
        })
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    //透明度
    setOpacity : function(opacity){
        cc.Node.prototype.setOpacity.call(this, opacity);
        // this._super(opacity);

        this.mNumSprite.setOpacity(opacity);
        this.mFlagSprite.setOpacity(opacity);
    },

    //设置牌的num
    setCardNum : function(cardNum){
        var isExist = mo.kwxCardHelper.getCardNumIsBasicLaw(cardNum);
        if (isExist){
            this.mCardNum = cardNum;
        }else{
            this.mCardNum = 0;
        }
    },

    //牌的类型 kind 1:inhand 2:deal 3:openShow 4:openHide
    setCardKind : function(seat, kind, qiangzhi3ren){
        this.mSeat = seat;
        this.mKind = kind;
        this.mNumSprite.setScale(1);
        this.mNumSprite.setRotation(0);
        this.mCardZOrder = -1;
        this.setAnchorPoint(cc.p(0.5, 0));

        //还原颜色
        if (seat == 0 && kind == 1){

        }else {
            this.resetCardColor();
        }

        //还原flag
        if (seat != 0 || kind != 1){
            this.setFlag(-1);
        }

        //还原透明度
        this.setOpacity(255);

        //设置背景
        this.setCardBg(qiangzhi3ren);

        if (qiangzhi3ren != undefined && qiangzhi3ren == true){

        }else{
            //2人场对面玩家的布局
            if (mo.gameMsgManager.kwxGameModel == 2 && seat != 0){
                if (kind == 1){
                    if (this.mCardNum == 0){
                        this.mNumSprite.setVisible(false);
                        this.setScale(mo.kwxCardHelper.oppoInhandCardScale);
                    }else{
                        this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                        this.mNumSprite.setVisible(true);
                        // this.mNumSprite.setScaleY(-1);
                        this.setNumSpritePos(cc.p(0, 16));
                        this.setScale(mo.kwxCardHelper.oppoInhandCardScale);
                    }

                }else if (kind == 2){

                    this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                    this.mNumSprite.setVisible(true);
                    this.setNumSpritePos(cc.p(0, 16));

                    this.setScale(mo.kwxCardHelper.dealScale);

                }else if (kind == 3){

                    this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                    this.mNumSprite.setVisible(true);
                    // this.mNumSprite.setScaleY(-1);
                    this.setNumSpritePos(cc.p(0, 16));
                    this.setScale(mo.kwxCardHelper.oppoOpenCardScale);

                }else if (kind == 4){

                    this.mNumSprite.setVisible(false);
                    this.setNumSpritePos(cc.p(0, 0));
                    this.setScale(mo.kwxCardHelper.oppoOpenCardScale);

                }
                return;
            }
        }

        if (seat == 0){
            if (kind == 1){

                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.mNumSprite.setScale(1);
                this.setNumSpritePos(cc.p(0, -13));
                this.setScale(mo.kwxCardHelper.inhandCardScale);

            }else if (kind == 2){

                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 16));
                this.setScale(mo.kwxCardHelper.dealScale);

            }else if (kind == 3){

                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 16));
                this.setScale(mo.kwxCardHelper.openCardScale);

            }else if (kind == 4){

                this.mNumSprite.setVisible(false);
                this.setNumSpritePos(cc.p(0, 0));
                this.setScale(mo.kwxCardHelper.openCardScale);

            }
        }else {
            if (seat == 1){
                this.mNumSprite.setRotation(270);
            }else if(seat == 2){
                this.mNumSprite.setRotation(90);
            }

            if (kind == 1){
                if (this.mCardNum == 0){
                    this.mNumSprite.setVisible(false);
                    this.setScale(mo.kwxCardHelper.leftInhandCardScale);

                    if (seat == 1){
                        var scaleX = -1 * this.getScaleX();
                        var scaley = this.getScaleX();
                        this.setScale(scaleX, scaley);
                    }
                }else{
                    this.setAnchorPoint(cc.p(0.5, 0.238));

                    this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                    this.mNumSprite.setVisible(true);
                    this.setNumSpritePos(cc.p(0, 15));
                    this.setScale(mo.kwxCardHelper.leftInhandShowCardScale);
                }

            }else if (kind == 2){

                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 15));

                this.setScale(mo.kwxCardHelper.leftDealScale);

            }else if (kind == 3){

                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 15));
                this.setScale(mo.kwxCardHelper.leftOpenCardScale);

            }else if (kind == 4){

                this.mNumSprite.setVisible(false);
                this.setScale(mo.kwxCardHelper.leftOpenCardScale);

            }
        }
    },

    //牌的类型 kind 1:inhand 2:deal 3:openShow 4:openHide
    setCardBg : function(qiangzhi3ren){
        if (this.mSeat == null || this.mKind == null || this.mCardNum == null){
            return
        }
        var seat = this.mSeat;
        var kind = this.mKind;

        if (qiangzhi3ren != undefined && qiangzhi3ren == true){

        }else {
            //2人场对面玩家的布局
            if (mo.gameMsgManager.kwxGameModel == 2 && seat != 0) {
                if (kind == 1) {
                    if (this.mCardNum == 0) {
                        this.setSpriteFrame(this.getImageFileName("handHide.png"));

                    } else {
                        this.setSpriteFrame(this.getImageFileName("open.png"));

                    }
                } else if (kind == 2) {
                    this.setSpriteFrame(this.getImageFileName("open.png"));

                } else if (kind == 3) {
                    this.setSpriteFrame(this.getImageFileName("open.png"));

                } else if (kind == 4) {
                    this.setSpriteFrame(this.getImageFileName("hide.png"));

                }
                return;
            }
        }

        //3人场布局
        if (seat == 0){
            if (kind == 1){
                this.setSpriteFrame(this.getImageFileName("hand.png"));

            }else if (kind == 2){
                this.setSpriteFrame(this.getImageFileName("open.png"));

            }else if (kind == 3){
                this.setSpriteFrame(this.getImageFileName("open.png"));

            }else if (kind == 4){
                this.setSpriteFrame(this.getImageFileName("hide.png"));

            }
        }else {
            if (kind == 1){
                if (this.mCardNum == 0){
                    this.setSpriteFrame(this.getImageFileName("leftHand.png"));

                }else{
                    this.setSpriteFrame(this.getImageFileName("leftOpen.png"));

                }
            }else if (kind == 2){
                this.setSpriteFrame(this.getImageFileName("leftOpen.png"));

            }else if (kind == 3){
                this.setSpriteFrame(this.getImageFileName("leftOpen.png"));

            }else if (kind == 4){
                this.setSpriteFrame(this.getImageFileName("leftHide.png"));

            }
        }
    },

    setNumSpritePos : function(p) {
        var size = this.getContentSize();
        this.mNumSprite.setPosition(cc.p(size.width / 2 + p.x, size.height / 2 + p.y));

        // var scaleX = this.mNumSprite.getScaleX();
        // var scaleY = this.mNumSprite.getScaleY();
        // this.mNumSprite.setScale(scaleX * 1.05, scaleY * 1.05);
    },

    //返回zorder
    getCardZ : function(){
        var z = 0;
        if (mo.kwxTable.mKwxCardLayer.mSelectMoveSprite == this){
            //滑动出牌的牌放在最上面
            z = mo.gameSize.y + mo.gameSize.y;
        }else{
            if (this.mCardZOrder < 0){
                if (this.mKind == 2){
                    z = parseInt(mo.gameSize.y - this.getPositionY()) + mo.gameSize.y;
                }else{
                    z = parseInt(mo.gameSize.y - this.getPositionY());
                }
            }else{
                z = this.mCardZOrder;
            }
        }
        return z;
    },

    setCardColor : function(color){
        this.mIsColor = true;
        this.setColor(color);
        this.mNumSprite.setColor(color);
        this.mFlagSprite.setColor(color);
    },

    resetCardColor : function(){
        this.mIsColor = false;
        this.setColor(cc.color(255, 255, 255));
        this.mNumSprite.setColor(cc.color(255, 255, 255));
        this.mFlagSprite.setColor(cc.color(255, 255, 255));
    },

    getImageFileName : function(name){
        var bgStr = "";
        if (mo.gameUserInfo.mGameKwxCardIndex == "1"){
            bgStr = "cardBg1_";
        }else if (mo.gameUserInfo.mGameKwxCardIndex == "2"){
            bgStr = "cardBg2_";
        }else{
            bgStr = "cardBg3_";
        }

        return bgStr + name;
    },

    //-1:不显示 0:危险 1:亮 2:听 3:扣
    setFlag : function(type){
        if (type == -1){
            this.mFlagSprite.setVisible(false);
        }else{
            var flagFile = "";
            if (type == 0){
                flagFile = "flag_weixian.png";
            }else if (type == 1){
                flagFile = "flag_liang.png";
            }else if (type == 2){
                flagFile = "flag_ting.png";
            }else if (type == 3){
                flagFile = "flag_kou.png";
            }
            this.mFlagSprite.setVisible(true);
            this.mFlagSprite.setSpriteFrame(flagFile);
        }
    }
});